import storage  from '@/util/storage'
import Mapcar   from './Mapcar'
import MapPathalarm from './MapPathalarm'
import Maprail  from './Maprail'
import simg from './images/start.png'
import eimg from './images/end.png'
//轨迹操作对象
class Mappath {

	map = null
	gps = []
	points = []
	path = null
	speedRange = []
	lineColor = ['#3AFF20','#7d7afb','#F50527']
	car = null
	alarmslist = []
	alarmsObjs = {}
	fenceslist = []
	fencesObjs = {}
	stopslist = []
	stopsObjs = {}

	constructor({map,gps,info,alarms,fences,stops}){
		this.map = map;
		this.gps = gps;
		this.info = info;
		this.alarms = alarms;
		this.fences = fences;
		this.stops = stops;
		this.points = gps.map(v=>[v.lng,v.lat]);
		this.speedRange = [7,300];
		this.pathSimplifierIns = new PathSimplifier({
        zIndex: 100,
        map: map, //所属的地图实例
        getPath:(pathItem)=>{
        	return pathItem.path;
        },
        getHoverTitle:()=>null,
        renderOptions: {
        		eventSupport:false,
        		eventSupportInvisible:false,
            getPathStyle:(pathItem, zoom)=>{
            	return  {
	            		pathLineStyle:{
	                strokeStyle: pathItem.pathData.strokeColor,
	                lineWidth: 6,
	                dirArrowStyle: true
		            }
	          	}
            }
        }
    });
    

		this.init();
	}

	init(){
		const lineOpts = this._partitionPath(this.gps);
		this._addStart(this.gps[0]);
		this._addEnd(this.gps[this.gps.length-1]);
		this._drawPath(lineOpts);
		this.car = new Mapcar({map:this.map,gps:this.gps[0],isWindow:false,info:this.gps[0]});
		this.car.movePath(this.gps,this.info);
    // this.car = this.pathSimplifierIns.createPathNavigator(0,{loop: false,speed:10000});
		
		if(this.alarms&&this.alarms.length){
			this._initAlarms();
		}
		if(this.fences&&this.fences.length){
			this._initFence();
		}
		if(this.stops&&this.stops.length){
			this._initStops();
		}
	}

	_initAlarms(){
		this.alarms.forEach(v=>{
			let s = new MapPathalarm({map:this.map,gps:v,info:v,type:v.type});
			this.alarmslist.push(s);
			this.alarmsObjs[v.id] = s;
		});
	}

	_initFence(){
		this.fences.forEach(v=>{
			this.fenceslist.push(new Maprail({map:this.map,type:v.model,data:v.elecFenceLocList,label:v.name}));
		});
	}

	_initStops(){
		this.stops.forEach(v=>{
			v.lng = v.stopLng;
			v.lat = v.stopLat;
			v.alarmTypeId = 4;
			let s = new MapPathalarm({map:this.map,gps:v,info:v});
			this.stopslist.push(s);
			this.stopsObjs[v.id] = s;
		});
	}

	_addStart(lnglat){
		return new AMap.Marker({
			map:this.map,
			icon:new AMap.Icon({
            imageSize:new AMap.Size(32/1.5,53/1.5),
            size: new AMap.Size(32, 53),
            image: simg
        }),
			zIndex:161,
			position:[lnglat.lng,lnglat.lat]
		})
	}

	_addEnd(lnglat){
		return new AMap.Marker({
			map:this.map,
			icon:new AMap.Icon({
						imageSize:new AMap.Size(32/1.5,53/1.5),
            size: new AMap.Size(32, 53),
            image: eimg
        }),
			zIndex:161,
			position:[lnglat.lng,lnglat.lat]
		});
	}

	_partitionPath(gps){
		let lineOpts = [];
		gps.forEach((v,i)=>{
			let level = -1;
			for(let n = 0;n<=this.speedRange.length;n++){
				let x = n?this.speedRange[n-1]:0;
				let y = n===this.speedRange.length?Number.POSITIVE_INFINITY:this.speedRange[n];
				if(v.gpsSpeed >= x&&v.gpsSpeed < y){
					level = n;
					break;
				}
			}
			let _index = lineOpts.length-1>-1?lineOpts.length-1:0;
			let line = lineOpts[_index] = lineOpts[_index]||{};

			if((line.level!==undefined&&line.level!==level)||(line.level===undefined)){
				if(line.level!==undefined){
					line.gps.push(v);
					line = lineOpts[_index+1] = {};
				}
				line.level = level;
				line.gps = [];
				line.min = i;
			}
			line.max = i;
			line.gps.push(v);
		});
		return lineOpts;
	}

	remove(){
		this.pathSimplifierIns.setData([]);
	}

	_drawPath(lineOpts){
		// let a = {
		// 	path:this.gps.map(p=>[p.lng,p.lat]),
		// 	strokeColor:'rgba(0,0,0,0)'
		// };
		// let b = lineOpts.map(v=>{
		// 	return {
		// 	path:v.gps.map(p=>[p.lng,p.lat]),
		// 	strokeColor:this.lineColor[v.level]
		// }})
		// this.pathSimplifierIns.setData([a,...b]);
		// 
		let b = lineOpts.map(v=>{
			return {
			path:v.gps.map(p=>[p.lng,p.lat]),
			strokeColor:this.lineColor[v.level]
		}})
		this.pathSimplifierIns.setData(b);
	}
}
export default Mappath;